package com.android;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.android.floatwindowpermission.R;

/**
 * Created by
 * author:  cwy.
 * date:    2017/12/21.  12:55.
 * prj:     FloatWindowPermission.
 */


public class MainA extends AppCompatActivity {

    private WindowManager mWindowManager;
    private RelativeLayout mFloatLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction("RE_SCAN");
        registerReceiver(sysBlueteState, filter);
        findViewById(R.id.btn_show_or_apply).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BTUtils.turnOnBluetooth();
            }
        });

        findViewById(R.id.btn_dismiss).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BTUtils.turnOffBluetooth();
            }
        });
    }


    BroadcastReceiver sysBlueteState = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.d("sysBlueteState", "onReceive: " + action);
            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                        BluetoothAdapter.ERROR);
                switch (state) {
                    case BluetoothAdapter.STATE_OFF:
                        Log.d("sysBlueteState", "onReceive: +off");
                        Toast.makeText(context, "关闭蓝牙", Toast.LENGTH_SHORT).show();
                        dialogs();
                        break;
                    case BluetoothAdapter.STATE_ON:
                        Toast.makeText(context, "打开蓝牙", Toast.LENGTH_SHORT).show();
                        break;
                }
            } else if (action.equals("RE_SCAN")) {
            }
        }
    };


    public void dialogs() {
        WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
//获取的是WindowManagerImpl.CompatModeWrapper
        mWindowManager = (WindowManager) getApplication().getSystemService(getApplication().WINDOW_SERVICE);
//设置window type
        wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
//设置浮动窗口不可聚焦（实现操作除浮动窗口外的其他可见窗口的操作）
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        wmParams.format = PixelFormat.TRANSLUCENT;
//调整悬浮窗显示的停靠位置为左侧置顶
        wmParams.gravity = Gravity.LEFT | Gravity.TOP;
// 以屏幕左上角为原点，设置x、y初始值，相对于gravity
        wmParams.x = 0;
        wmParams.y = 0;

//设置悬浮窗口长宽数据
        wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        wmParams.height = WindowManager.LayoutParams.MATCH_PARENT;

        LayoutInflater inflater = LayoutInflater.from(getApplication());
//获取浮动窗口视图所在布局
        mFloatLayout = (RelativeLayout) inflater.inflate(R.layout.ll, null);
//添加mFloatLayout
        mWindowManager.addView(mFloatLayout, wmParams);
        mFloatLayout.findViewById(R.id.btn_dismiss).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BTUtils.turnOnBluetooth();
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        mWindowManager.removeView(mFloatLayout);
    }
}
